package com.kezesong.leasing.controller.admin;

import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AddGoodsColorReqBody;
import com.kezesong.leasing.controller.request.admin.ModifyGoodsColorReqBody;
import com.kezesong.leasing.entity.GoodsColorEntity;
import com.kezesong.leasing.service.GoodsColorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class GoodsColorManagerController extends BaseController {


    @Autowired
    private GoodsColorService goodsColorService;

    @PostMapping("/getAllGoodsColorList.do")
    @ApiOperation(value = "getAllGoodsColorList", notes = "获取全部的商品颜色列表")
    public ResponseResult<List<GoodsColorEntity>> getAllGoodsColorList(){
        List<GoodsColorEntity> goodsColorList = goodsColorService.getGoodsColorList();

        if (null == goodsColorList || goodsColorList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsColorEntity>> listResponseResult = buildSucessResponse(goodsColorList);
            return listResponseResult;
        }

    }

    @GetMapping("/getGoodsColorListByGoodsId/{goodsId}")
    @ApiOperation(value = "getGoodsColorListByGoodsId", notes = "根据商品id获取某个商铺的所有颜色")
    public ResponseResult<List<GoodsColorEntity>> getGoodsColorListByGoodsId(@PathVariable("goodsId") int goodsId){
        List<GoodsColorEntity> goodsColorList = goodsColorService.getGoodsColorList(goodsId);
        if (null == goodsColorList || goodsColorList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsColorEntity>> listResponseResult = buildSucessResponse(goodsColorList);
            return listResponseResult;
        }
    }

    @PostMapping("/addGoodsColor.do")
    @ApiOperation(value = "addGoodsColor", notes = "管理员添加商品颜色")
    public ResponseResult<String> addGoodsColor(@RequestBody AddGoodsColorReqBody reqBody){
        GoodsColorEntity goodsColorEntity = new GoodsColorEntity();
        BeanUtils.copyProperties(reqBody, goodsColorEntity);
        int rows = goodsColorService.addGoodsColor(goodsColorEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @GetMapping("/deleteGoodsColor/{colorId}")
    @ApiOperation(value = "deleteGoodsColor", notes = "管理员删除商品颜色")
    public ResponseResult<String> deleteGoodsColor(@PathVariable("colorId") int colorId){
        int rows = goodsColorService.deleteGoodsColor(colorId);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @PostMapping("/modifyGoodsColor.do")
    @ApiOperation(value = "modifyGoodsColor", notes = "管理员修改商品颜色")
    public ResponseResult<String> modifyGoodsColor(@RequestBody ModifyGoodsColorReqBody reqBody){
        GoodsColorEntity goodsColorEntity = new GoodsColorEntity();
        BeanUtils.copyProperties(reqBody,goodsColorEntity);
        int rows = goodsColorService.modifyGoodsColor(goodsColorEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }
}
